<?php
  // no direct access
  defined( '_JEXEC' ) or die( 'Restricted access' );
  jimport('joomla.application.component.controller');
  
  class PlayersController extends JController
  {  
    function display(){
      
      $document =& JFactory::getDocument();
      $view = & $this->getView($this->_name, $document->getType());
      
      $player  = $this->getModel('player', 'playermodel');
      $person  = $this->getModel('person', 'personmodel');
      $team    = $this->getModel('team', 'teammodel');
      
      $players     = array();
      $players     = $player->get();
      $dir_players = array();
      
      if($players)
      {
        foreach($players as $p){
          
          $p->fullname = $person->get_fullname($p->person_id);
          $p->team     = $team->get($p->team_id);
          $index       = strtoupper($p->fullname[0]);
          if(!isset($dir_players[$index]))
            $dir_players[$index] = array();
          $dir_players[$index][$p->fullname] = $p;
          ksort($dir_players[$index]);
        }        
      }
      
      ksort($dir_players);
      $view->assign("dir_players", $dir_players);
      
      $view->display();
    }    
    
    function display_player(){
      
      $document =& JFactory::getDocument();
      $view = & $this->getView($this->_name, $document->getType());  
      
      $mteam     = $this->getModel('team', 'teammodel');
      $mplayer   = $this->getModel('player', 'playermodel');
      $person    = $this->getModel('person', 'personmodel');
      $position  = $this->getModel('position', 'positionmodel');
      $pic       = $this->getModel('pic', 'picmodel');
      $pgs     = $this->getModel('playergamestat', 'playergamestatmodel');
      $season    = $this->getModel('season', 'seasonmodel');
        
      $id = JRequest::getVar('id');
      if($id){
        $avg_stats    = array();
        $stats        = array();
        $seasons      = array();
        
        $player           = $mplayer->get($id);
        $player->person   = $person->get($player->person_id);
        $player->position = $position->get($player->position_id);
        
        $pics = $pic->get_pics_by_instance('PLAYERS', $player->id);
        if($pics){
          foreach($pics as $p){
            if($p->id == $player->pic_id)
              $player->pic  = $p;
            else
              $player->feature_pic = $p;
          }
        }
        
        foreach($season->get() as $s){
            # stats
          $tmp_result = $pgs->get_season_stats_by_player_id($player->id, $s->id);
          if($tmp_result)
            $stats[$s->id] = $this->prepare_stats($player, $tmp_result);
          
            # avg stats
          $tmp_result = $pgs->get_avg_season_stats($player->id, $s->id);
          if($tmp_result){
            if(!isset($seasons[$s->id]))
              $seasons[$s->id] = $s->name;
            $avg_stats[$s->id] = $tmp_result;
            
          }
        }
        
        $player->avg_stats  = $avg_stats;
        $player->stats      = $stats;
        $view->assign("player", $player);
        $view->assign("seasons", $seasons);
      }
      
      #teams
      $teams = array();
      foreach($mteam->get() as $team)
        $teams[$team->id] = $team;
        
      $view->assign("teams", $teams);
      $view->display_player();
    }
    
    function compare_players(){
      
      $document =& JFactory::getDocument();
      $view = & $this->getView($this->_name, $document->getType());  
      
      $mplayer   = $this->getModel('player', 'playermodel');
      $person    = $this->getModel('person', 'personmodel');
      $position  = $this->getModel('position', 'positionmodel');
      $pgs     = $this->getModel('playergamestat', 'playergamestatmodel');
      $season    = $this->getModel('season', 'seasonmodel');
      $pic       = $this->getModel('pic', 'picmodel');
      
      $id       = JRequest::getVar('id');
      $other_id = JRequest::getVar('others');
      
      if(is_numeric($id) && is_numeric($other_id)){
        
        $hme_player           = $mplayer->get($id);
        $hme_player->person   = $person->get($hme_player->person_id);
        $hme_player->position = $position->get($hme_player->position_id);
        if($hme_player->pic_id)
          $hme_player->pic  = $pic->get($hme_player->pic_id);
        
        $awy_player               = $mplayer->get($other_id);
        $awy_player->person   = $person->get($awy_player->person_id);
        $awy_player->position = $position->get($awy_player->position_id);
        if($awy_player->pic_id)
          $awy_player->pic  = $pic->get($awy_player->pic_id);
        
        $home     = array();
        $away     = array();
        $seasons  = array();
        foreach($season->get() as $s){
          
          if(!isset($seasons[$s->id]))  //set seasons
            $seasons[$s->id] = $s->name;
          
          // FOR HOME
          $tmp_result = $pgs->get_avg_season_stats($id, $s->id);
          if($tmp_result)  //add only if exists
            $home[$s->id] = $tmp_result;
          
          
          // FOR AWAY
          $tmp_result = $pgs->get_avg_season_stats($other_id, $s->id);
          if($tmp_result)  //add only if exists
            $away[$s->id] = $tmp_result;
          
        }
        
        $hme_player->player_game_stats = $home;
        $awy_player->player_game_stats = $away;
        
        $view->assign("hme_player", $hme_player);
        $view->assign("awy_player", $awy_player);
        $view->assign("seasons", $seasons);
      }
      
      $view->display_compare();
    }
    
    function ajx_select_players(){
      
      $team_id = JRequest::getVar('team_id');
      $player  = $this->getModel('player', 'playermodel');
      $person  = $this->getModel('person','personmodel');
	  
      $players = $player->get_by_team_id($team_id);
	  for($i=0;$i<count($players);$i++){
        $p   = $players[$i];
        $tmp = $person->get($p->person_id);
        $p->name = $tmp->lastname.", ".$tmp->firstname;        
      }
	  
	  if(!$players)
	    $players = array();

      $result  = JHTML::_('select.genericlist', $players, 'player_id', null, 'id', 'name', null);
      echo $result;      
    }
  
    private function prepare_stats($player, $stats){
    
      $mschedule    = $this->getModel('schedule', 'schedulemodel');
      $mteam        = $this->getModel('team', 'teammodel');
      
      foreach($stats as $stat){
        
        $schedule = $mschedule->get($stat->schedule_id);
        
        $o_team_id = $schedule->first_team_id;
        if($o_team_id == $player->team_id)
          $o_team_id = $schedule->second_team_id;
        
        
        $stat->schedule = $schedule->schedule;
        $stat->opponent_team_id = $o_team_id;
      }
      
      return $stats;
    }
  }
?>
